Completed
Push — master ( edfbd4...6aa1a8 )
by
unknown
02:12
created

lateKeys   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 31
rs 8.8571
c 1
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B abe-get-select-template-keys.js ➔ ... ➔ ??? 0 27 1
1
import path from 'path'
2
import fse from 'fs-extra'
3
import {execFile} from 'child_process'
4
import {
5
  fileUtils,
0 ignored issues
show
Unused Code introduced by
The variable fileUtils seems to be never used. Consider removing it.
Loading history...
6
  FileParser,
0 ignored issues
show
Unused Code introduced by
The variable FileParser seems to be never used. Consider removing it.
Loading history...
7
  Util,
8
  Sql,
9
  cleanSlug,
0 ignored issues
show
Unused Code introduced by
The variable cleanSlug seems to be never used. Consider removing it.
Loading history...
10
  getTemplate,
0 ignored issues
show
Unused Code introduced by
The variable getTemplate seems to be never used. Consider removing it.
Loading history...
11
  save,
0 ignored issues
show
Unused Code introduced by
The variable save seems to be never used. Consider removing it.
Loading history...
12
  config,
13
  log,
14
  Hooks,
0 ignored issues
show
Unused Code introduced by
The variable Hooks seems to be never used. Consider removing it.
Loading history...
15
  removeDuplicateAttr,
0 ignored issues
show
Unused Code introduced by
The variable removeDuplicateAttr seems to be never used. Consider removing it.
Loading history...
16
  Manager
0 ignored issues
show
Unused Code introduced by
The variable Manager seems to be never used. Consider removing it.
Loading history...
17
} from '../../cli'
18
19
var traverseFileSystem = function (currentPath, arr) {
0 ignored issues
show
Unused Code introduced by
The parameter arr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
20
  var res = []
21
  var files = fse.readdirSync(currentPath)
22
  for (var i in files) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
23
    var currentFile = currentPath + '/' + files[i]
24
    var stats = fse.statSync(currentFile)
25
    if (stats.isFile()) {
26
      if (currentFile.indexOf(config.files.templates.extension) > -1) {
27
        res.push(currentFile)
28
      }
29
    }
30
    else if (stats.isDirectory()) {
31
      res = res.concat(traverseFileSystem(currentFile))
32
    }
33
  }
34
  return res
35
}
36
37
var findTemplates = function(templatesPath) {
38
  var p = new Promise((resolve, reject) => {
0 ignored issues
show
Unused Code introduced by
The parameter reject is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
39
    let templatesList = traverseFileSystem(templatesPath)
40
    resolve(templatesList)
41
  })
42
43
  return p
44
}
45
46
/**
47
 * Get columns and where.left ids of a select statement
48
 *
49
 * select title, image from ../ where template=""
50
 *
51
 * return [title, image, template]
52
 * 
53
 * @param  {Array} templatesList ["article.html", "other.html"]
54
 * @return {Promise}
55
 */
56
57
var recurseWhereVariables = function (where) {
58
  var ar = []
59
60
  // console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
61
  // console.log('where', where.left)
62
  switch(where.operator) {
63
  case 'AND':
64
    var arLeft = recurseWhereVariables(where.left)
65
    var arRight = recurseWhereVariables(where.right)
66
    return arLeft.concat(arRight)
67
    break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
68
  case 'OR':
69
    var arLeft = recurseWhereVariables(where.left)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable arLeft already seems to be declared on line 64. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
70
    var arRight = recurseWhereVariables(where.right)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable arRight already seems to be declared on line 65. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
71
    return arLeft.concat(arRight)
72
    break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
73
  case 'IN':
74
    break
75
  case 'NOT IN':
76
    break
77
  default:
78
    return [where.left.column]
79
    break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
80
  }
81
82
  return ar
83
}
84
85
var findRequestColumns = function(templatesList) {
86
  var whereKeysCheck = {}
0 ignored issues
show
Unused Code introduced by
The variable whereKeysCheck seems to be never used. Consider removing it.
Loading history...
87
  var whereKeys = []
88
  var p = new Promise((resolve, reject) => {
0 ignored issues
show
Unused Code introduced by
The parameter reject is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
89
    let util = new Util()
90
    Array.prototype.forEach.call(templatesList, (file) => {
91
      var template = fse.readFileSync(file, 'utf8')
92
      var matches = util.dataRequest(template)
93
94
      Array.prototype.forEach.call(matches, (match) => {
95
        var obj = Util.getAllAttributes(match[0], {})
96
        obj = Util.sanitizeSourceAttribute(obj, {})
97
        
98
        var type = Sql.getSourceType(obj.sourceString)
99
100
        switch (type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
101
        case 'request':
102
          var request = Sql.handleSqlRequest(obj.sourceString, {})
103
          if(typeof request.columns !== 'undefined' && request.columns !== null) {
104
            Array.prototype.forEach.call(request.columns, (column) => {
105
              whereKeys.push(column)
106
            })
107
          }
108
          if(typeof request.where !== 'undefined' && request.where !== null) {
109
            whereKeys = whereKeys.concat(recurseWhereVariables(request.where))
110
          }
111
        }
112
      })
113
    })
114
    whereKeys = whereKeys.filter(function (item, pos) {return whereKeys.indexOf(item) == pos})
115
    resolve(whereKeys)
116
  })
117
118
  return p
119
}
120
121
var getSelectTemplateKeys = function(templatesPath) {
122
  var p = new Promise((resolve, reject) => {
123
    findTemplates(templatesPath)
124
      .then((templatesList) => {
125
126
        findRequestColumns(templatesList)
127
          .then((whereKeys) => {
128
            resolve(whereKeys)
129
          },
130
          () => {
131
            console.log('findRequestColumns reject')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
132
            reject()
133
          })
134
          .catch((e) => {
135
            console.error('getSelectTemplateKeys', e)
136
            reject()
137
          })
138
      },
139
      () => {
140
        console.log('findTemplates reject')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
141
        reject()
142
      })
143
      .catch((e) => {
144
        console.error('getSelectTemplateKeys', e)
145
        reject()
146
      })
147
148
  })
149
150
  return p
151
}
152
153
export default getSelectTemplateKeys